package com.leizhi.demo.monitor.utils;

import com.leizhi.demo.monitor.pojo.Host;
import gov.nist.javax.sip.address.AddressImpl;
import gov.nist.javax.sip.address.SipUri;

import javax.sip.header.FromHeader;
import javax.sip.header.ViaHeader;
import java.util.UUID;

/**
 * @Description SIP 工具类
 * @Version 1.0.0-SNAPSHOT
 * @Author leizhi
 * @Date 2025/4/27 10:44
 */
public class SipUtil {

    /* 获取设备发送的 SIP 服务器地址 */
    public static Host getHost(ViaHeader viaHeader) {
        String received = viaHeader.getReceived();
        int port = viaHeader.getRPort();
        if (received.isEmpty() || port == -1) {
            received = viaHeader.getHost();
            port = viaHeader.getPort();
        }

        return new Host(received, port);
    }

    /* 获取设备 ID */
    public static String getDeviceId(FromHeader fromHeader) {
        AddressImpl address = (AddressImpl) fromHeader.getAddress();
        SipUri uri = (SipUri) address.getURI();
        return uri.getUser();
    }

    /* 生成唯一的 ID，并且符合 SIP 协议的格式 */
    public static String uuid() {
        return UUID.randomUUID().toString().replace("-", "").toUpperCase();
    }

}
